<?php
/*
	PookiWiki - A wiki unlike the rest.
	Copyright (C) 2012 Jordan Craw

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License
	as published by the Free Software Foundation; either version 2
	of the License, or (at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

	class PookiXMLSkeleton
	{
	
	}

	class PookiXML
	{
		protected $XML_string;
		protected $SimpleXML_instance;
		protected $base_element_array;
		protected $skeleton_instance;
		
		
		protected function objectize($rendered_array, PookiXMLSkeleton &$skeleton)
		{
			$base_element = $skeleton->base_element;
			$rest_array = $rendered_array[$base_element];
			$items = array_keys($rest_array);
			
			foreach($items as $item_key)
			{
				$skeleton->$item_key = (object)$rest_array[$item_key];
			}
			$skeleton = (object)$rest_array;
			unset($skeleton->array_contents); // Neater way of unsetting that annoyance.
			return (object)$rest_array; // Prevent recursion
		}
		
		protected function expandSkeletonSub($this_array)
		{
			if(is_a($this_array,"SimpleXMLElement"))
			{
				$this_array = array_map(array($this,"expandSkeletonSub"),(array)$this_array);
				return (array)$this_array;
			}
			if(count($this_array) ==1)
			{
				return $this_array;
			}
			else
			{
				return "";
			}
		}
		
		protected function expandSkeleton($base_element,$children)
		{
			$this->base_element_array[$base_element] = array();
			$elements = array_map(array($this,"expandSkeletonSub"),(array)$children);
			return $elements;
		}
		
		protected function constructSkeleton()
		{
			$base_element = $this->SimpleXML_instance->getName();

			if($base_element) 
			{
				$element = "base_element";
				$skeleton = new PookiXMLSkeleton();
				$skeleton->$element = $base_element;
				
				if(count($this->SimpleXML_instance->children()))
				{
					$children = $this->SimpleXML_instance->children();
					$contents = $this->expandSkeleton($base_element,$children);
					$count = count($base_element);

					$contents = array
					(
						$base_element => $contents
					);
					

					
					$skeleton->array_contents = $contents;
					$skeleton->object_contents = $this->objectize($contents,$skeleton);
					return $skeleton;
				}
			}
		}
		
		
		public function __construct($path)
		{
		
			$file_instance 		=		new PookiFile($path);
			$file_reader		=		new PookiFileReader($file_instance);
			$contents			=		$file_reader->contents_string;
			
			if(strlen($contents) or !is_null($contents))
			{
				$this->SimpleXML_instance = $XMLInstance = new SimpleXMLElement($contents);
		
				// Create a skeleton Class which contains the elements 
				$this->skeleton_instance = $this->constructSkeleton();
			}
			else
			{
				throw new PookiFileException();
			}
			// Ends here, as __get() means that this XML instance acts as a mirror element for the skeleton.
		}
		
		public function __get($variable_name)
		{
			if(!is_null($this->skeleton_instance))
			{
				$array_contents = $this->skeleton_instance;
				
				if(isset($array_contents->$variable_name))
				{
					return $array_contents->$variable_name;
				}
			}
		}
		
	}


?>